package com.today.usercenter.utils;

import android.text.TextUtils;

import com.today.lib.common.utils.MD5Utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by cgm on 18-6-20.
 */

public class InputUtils {
    /**
     * 手机号输入校验是否合法
     *
     * @param mobile
     * @return
     */
    public static boolean isLegalCellphone(String mobile) {
        Boolean isLegal = false;
        if (!TextUtils.isEmpty(mobile) && mobile.length() == 11 && checkCellphone(mobile)) {
            isLegal = true;
        }
        return isLegal;
    }

    /**
     * 手机号匹配
     *
     * @param cellphone
     * @return
     */
    private static boolean checkCellphone(String cellphone) {
        String regExp = "0?(13|14|15|17|18)[0-9]{9}";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(cellphone);
        return m.find();
    }

    public static String encodeUserPwd(String origin) {
        return MD5Utils.encode("ustv" + origin + "4aiaiaiai");
    }

    /**
     * 密码输入校验是否合法
     *
     * @param str
     * @return
     */
    public static boolean isLegalPwd(String str) {
        Boolean isLegal = false;
        if (!TextUtils.isEmpty(str) && !str.contains(" ") && str.length() > 5
                && str.length() < 17 && !isContainChinese(str)) {
            isLegal = true;
        }
        return isLegal;
    }

    /**
     * 是否包含中文
     *
     * @param str
     * @return
     */
    private static boolean isContainChinese(String str) {
        String regExp = "[\u4e00-\u9fa5]";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.find();
    }
}
